//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>


void threadfunc(Aura_PVoid pArg)
{
    Aura_ThreadId iThreadId;
    AuraThread_GetCurrentId(&iThreadId);
    while(1){
        Aura_printf("thread id:%d. arg:%d.\n", iThreadId, (int)pArg);
        AuraThread_Sleep((int)pArg, AURA_NULL);
        AuraThread_Yield();
    }
}

Aura_Bool AuraTest_ThreadSuspendAndResume()
{
    Aura_Handle hThread[5];
    Aura_ThreadId Aura_ThreadId[5];
    Aura_ThreadPriority priority = 0xFF;
    int i;
	Aura_ECode ec = AURA_NOERROR;

    AuraThread_Create(
        threadfunc,
        (Aura_PVoid)500,
        Aura_ThreadCreationFlag_Unstarted,
        AURA_NULL,
        &Aura_ThreadId[0]);
    Aura_printf("In main created thread :%d.\n", Aura_ThreadId[0]);

    ec = AuraThread_Open(Aura_ThreadId[0], &hThread[0]);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Open. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    ec = AuraThread_Resume(hThread[0]);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Resume. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    ec  =AuraThread_Suspend(hThread[0]);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Suspend. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    ec = AuraThread_Resume(hThread[0]);
    ec = AuraThread_Suspend(hThread[0]);
    ec = AuraThread_GetPriority(hThread[0], &priority);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_GetPriority. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }
    Aura_printf("Thread:%d priority:%d.\n", Aura_ThreadId[0], priority);

    priority++;
    ec = AuraThread_SetPriority(hThread[0], priority);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_SetPriority. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }
    ec = AuraThread_Resume(hThread[0]);

    for(i = 1; i < 5; i++){
        ec = AuraThread_Create(
            threadfunc,
            (Aura_PVoid)(i*500),
            0,
            &hThread[i],
            &Aura_ThreadId[i]);
        if (AURA_FAILED(ec)) {
            Aura_printf("File: %s Line: %d, AuraThread_Create. (ec = 0x%x)\n",
                __FILE__,__LINE__, ec);
            goto ErrorExit;
        }
    }

    for(i=0; i<5; i++){
        ec = AuraThread_Interrupt(hThread[i]);
	    if (AURA_FAILED(ec)) {
            Aura_printf("File: %s Line: %d, AuraThread_Interrupt. (ec = 0x%x)\n",
                __FILE__,__LINE__, ec);
            goto ErrorExit;
        }
    }

    for(i = 0; i < 5; i++){
        ec = AuraThread_Abort(hThread[i], 1);
	    if (AURA_FAILED(ec)) {
            Aura_printf("File: %s Line: %d, AuraThread_Abort. (ec = 0x%x)\n",
                __FILE__,__LINE__, ec);
            goto ErrorExit;
        }
        ec = AuraThread_Destroy(hThread[i]);
	    if (AURA_FAILED(ec)) {
            Aura_printf("File: %s Line: %d, AuraThread_Destroy. (ec = 0x%x)\n",
                __FILE__,__LINE__, ec);
            goto ErrorExit;
        }
    }
    return AURA_TRUE;

ErrorExit:
	return AURA_FALSE;
}


